import axios from 'axios';
import semver from 'semver';
import packjson from '../../../package.json';
import env from '../../../build_conf/env';
const moment = require("moment");

let util = {

};
util.title = function (title) {
    title = title || '秦皇岛企业网上金融服务平台';
    window.document.title = title;
};

util.inOf = function (arr, targetArr) {
    let res = true;
    arr.map(item => {
        if (targetArr.indexOf(item) < 0) {
            res = false;
        }
    });
    return res;
};

util.oneOf = function (ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.showThisRoute = function (permission, permissions) {
    if(!permissions) return false;
    if(permissions["100010"] == true) return true;
    return permissions[""+permission] == true;
};

util.getRouterObjByName = function (routers, name) {
    let routerObj = {};
    for(var i = 0; i < routers.length; i++) {
        var item = routers[i];
        if(item.name == name) {
            return item;
        }
        if(item.chlidren && item.chlidren.length > 0) {
            var temp = util.getRouterObjByName(item.children, name);
            if(temp.name == name) {
                return temp;
            }
        }
    }
    return routerObj;
};

util.handleTitle = function (vm, item) {
    return item.title;
};

util.setCurrentPath = function (vm, name) {
    var currentPathArr = [
        // {
        //     title: '中心首页',
        //     path: '/home',
        //     name: 'home_index'
        // }
    ];
    if(name != "home_index") {
        var routers = vm.$store.state.app.routers;
        for(var i = 0; i < routers.length; i++) {
            var parent = routers[i];
            if(parent.children) {
                for(var j = 0; j < parent.children.length; j++) {
                    var child = parent.children[j];
                    if(child.name == name) {
                        currentPathArr.push({title: parent.title});
                        currentPathArr.push({title: child.title});
                    }
                }
            }
        }
    }
    vm.$store.commit('setCurrentPath', currentPathArr);
    
    return currentPathArr;
};

util.openNewPage = function (vm, name, argu, query) {
    let pageOpenedList = vm.$store.state.app.pageOpenedList;
    let openedPageLen = pageOpenedList.length;
    let i = 0;
    let tagHasOpened = false;
    while (i < openedPageLen) {
        if (name === pageOpenedList[i].name) {  // 页面已经打开
            vm.$store.commit('pageOpenedList', {
                index: i,
                argu: argu,
                query: query
            });
            tagHasOpened = true;
            break;
        }
        i++;
    }
    if (!tagHasOpened) {
        let tag = vm.$store.state.app.tagsList.filter((item) => {
            if (item.children) {
                return name === item.children[0].name;
            } else {
                return name === item.name;
            }
        });
        tag = tag[0];
        if (tag) {
            tag = tag.children ? tag.children[0] : tag;
            if (argu) {
                tag.argu = argu;
            }
            if (query) {
                tag.query = query;
            }
            vm.$store.commit('increateTag', tag);
        }
    }
    vm.$store.commit('setCurrentPageName', name);
};

util.toDefaultPage = function (routers, name, route, next) {
    let len = routers.length;
    let i = 0;
    let notHandle = true;
    while (i < len) {
        if (routers[i].name === name && routers[i].children && routers[i].redirect === undefined) {
            route.replace({
                name: routers[i].children[0].name
            });
            notHandle = false;
            next();
            break;
        }
        i++;
    }
    if (notHandle) {
        next();
    }
};

util.fullscreenEvent = function (vm) {
    // 权限菜单过滤相关
    vm.$store.commit('updateMenulist');
};

util.checkUpdate = function (vm) {
    axios.get('https://api.github.com/repos/iview/iview-admin/releases/latest').then(res => {
        let version = res.data.tag_name;
        vm.$Notice.config({
            duration: 0
        });
        if (semver.lt(packjson.version, version)) {
            vm.$Notice.info({
                title: 'iview-admin更新啦',
                desc: '<p>iView-admin更新到了' + version + '了，去看看有哪些变化吧</p><a style="font-size:13px;" href="https://github.com/iview/iview-admin/releases" target="_blank">前往github查看</a>'
            });
        }
    });
};

util.getRuntimeUrl = function(url, sub_url) {
    if(!sub_url) sub_url = "";
    return "/" + (env === 'development' ? url + ".html" : url) + '#/' + sub_url;
}

// util.sleep = function(max_millisecond, callback) {
//     var start = Date.now();
//     while(true) {
//         var now = Date.now();
//         if(now - max_millisecond > start) break;
//         if(callback()) break;
//     }
// }

function merge(obj1, obj2) {
    for(var key in obj2) {
        if(typeof(obj2[key]) == "object") {
            if(typeof(obj1[key]) != "object") {
                obj1[key] = obj2[key];
            }else {
                merge(obj1[key], obj2[key]);
            }
        }else {
            obj1[key] = obj2[key];
        }
    }
}

util.merge = function() {
    var objs = arguments;
    if(objs.length == 0) return {};
    var obj1 = typeof(objs[0]) == "object" ? objs[0] : {};
    for(var i = 1; i < objs.length; i++) {
        if(typeof(objs[i]) == "object") {
            merge(obj1, objs[i]);
        }
    }
    return obj1;
};

Date.formatTimeStamp = function(timestamp, format) {
    return moment(parseInt(timestamp)).format(format || 'YYYY-MM-DD HH:mm');
}

export default util;
